package com.jiuzhi.server.mian;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.keepalive.KeepAliveFilter;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.jiuzhi.server.handler.KeepAliveMessageFactoryImpl;
import com.jiuzhi.server.handler.MinaLongConnServerHandler;

public class MinaLongConnServer {
	private static final int PORT = 8002;
	 private static final int IDELTIMEOUT = 30;  
	    /** 15秒发送一次心跳包 */  
	    private static final int HEARTBEATRATE = 15;  
	    /** 心跳包内容 */  
	    private static final String HEARTBEATREQUEST = "0x11";  
	    private static final String HEARTBEATRESPONSE = "0x12";  
	
	
	
    public void start()throws IOException{
       IoAcceptor acceptor = new NioSocketAcceptor();
       acceptor.getFilterChain().addLast("logger", new LoggingFilter());

       acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

       acceptor.setHandler(new MinaLongConnServerHandler());

       acceptor.getSessionConfig().setReadBufferSize(2048);
       acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,  
               IDELTIMEOUT);  
       KeepAliveMessageFactory heartBeatFactory = new KeepAliveMessageFactoryImpl();  
       //下面注释掉的是自定义Handler方式  
//     KeepAliveRequestTimeoutHandler heartBeatHandler = new   
//                             KeepAliveRequestTimeoutHandlerImpl();  
//     KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory,  
//             IdleStatus.BOTH_IDLE, heartBeatHandler);  
         
       KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory,  
               IdleStatus.BOTH_IDLE);  
 
       //设置是否forward到下一个filter  
       heartBeat.setForwardEvent(true);  /** 是否回发 */  
       //设置心跳频率      /** 发送频率 */
       heartBeat.setRequestInterval(HEARTBEATRATE);  
 
       acceptor.getFilterChain().addLast("heartbeat", heartBeat);  
       
       
       
       
       
       acceptor.bind(new InetSocketAddress(PORT));

       System.out.println("Listeningon port " + PORT);
    }
    
    public static void main(String[] args) throws IOException, InterruptedException {
		new  MinaLongConnServer().start();
/*		
		  for (int i = 0; i <100; i++) {
			  int count=Initialization.getInstance().getClientMap().keySet().size();
			  System.out.println("------------"+count);
			  if(count>=1){
				  IoSession sess = Initialization.getInstance().getClientMap().get("127.0.0.1") ;
				  sess.write(" SERVER发送到端消息："+new Date());
			  }
			  new Thread().sleep(5400);
		}*/
    }
}